package com.xsm.ppsecret.ui;

import android.content.Intent;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.xsm.ppsecret.R;
import com.xsm.ppsecret.util.CryptUtil;
import com.xsm.ppsecret.util.NativeHelper;
import com.xsm.ppsecret.util.ShareManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ForgetPasswordActivity extends AppCompatActivity {

    @BindView(R.id.menu_normal_action)
    TextView menuAction;
    @BindView(R.id.menu_normal_title)
    TextView menuTitle;
    @BindView(R.id.tv_question)
    TextView question;
    @BindView(R.id.et_forget_answer)
    EditText editAnswer;
    @BindView(R.id.tv_forget_remind)
    TextView remind;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        ButterKnife.bind(this);
        menuAction.setVisibility(View.GONE);
        menuTitle.setText("密码找回");
        String encryptedQuestion = ShareManager.getInstance().getEncryptedQuestion();
        byte[] bytes = CryptUtil.aesDecryptWithIv(Base64.decode(encryptedQuestion, Base64.NO_WRAP), NativeHelper.getAesKey(), NativeHelper.getAesIvKey());
        String s = new String(bytes);
        question.setText(s);
    }

    @Override
    public void onBackPressed() {
        startActivity(new Intent(this, LoginActivity.class));
    }

    @OnClick(R.id.menu_normal_back) void back() {
        startActivity(new Intent(this, LoginActivity.class));
    }

    @OnClick(R.id.tv_forget_confirm) void confirm() {
        String string = editAnswer.getText().toString();
        String encryptedAnswer = ShareManager.getInstance().getEncryptedAnswer();
        byte[] bytes = CryptUtil.aesDecryptWithIv(Base64.decode(encryptedAnswer, Base64.NO_WRAP), NativeHelper.getAesKey(), NativeHelper.getAesIvKey());
        String answer = new String(bytes);
        if (answer.equals(string)) {
            remind.setText("");
            startActivity(new Intent(this, RegistActivity.class));
        } else {
            remind.setText("问题回答错误!");
        }
    }

}
